/*
 * 描述： <描述>
 * 修改人： rain
 * 修改时间： 2016年8月9日
 * 项目： rainhy-servicelogger
 */
package com.rainhy.servicelogger.builder;

import com.rainhy.core.util.AssertUtils;
import com.rainhy.servicelogger.context.ServiceLoggerSession;
import com.rainhy.servicelogger.logger.ServiceLogDecorate;
import com.rainhy.servicelogger.logger.ServiceLogPersister;
import com.rainhy.servicelogger.logger.ServiceLogQuerier;
import com.rainhy.servicelogger.logger.ServiceLogger;

/**
 * 业务日志默认实现
 * 
 * @author rain
 * @version [版本号, 2016年8月9日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class ServiceLoggerImpl implements ServiceLogger {
    
    /** 业务日志装饰器 */
    private ServiceLogDecorate serviceLogDecorate;
    
    /** 业务日志查询器 */
    private ServiceLogQuerier serviceLogQuerier;
    
    /** 业务日志记录器 */
    private ServiceLogPersister serviceLogPersister;
    
    public ServiceLoggerImpl(
            ServiceLogDecorate serviceLogDecorate,
            ServiceLogQuerier serviceLogQuerier,
            ServiceLogPersister serviceLogPersister) {
        this.serviceLogDecorate = serviceLogDecorate;
        this.serviceLogPersister = serviceLogPersister;
        this.serviceLogQuerier = serviceLogQuerier;
    }
    
    @Override
    public Object find(String key) {
        return serviceLogQuerier.find(key);
    }
    
    @Override
    public Object getAttribute(String key) {
        return ServiceLoggerSession.getSession().getAttribute(key);
    }
    
    @Override
    public boolean isSupportQuery() {
        return serviceLogQuerier.isSupportQuery();
    }
    
    @Override
    public void log(Object log) {
        AssertUtils.notNull(log, "业务日志不能为 null");
        //利用业务日志装饰器，装饰日志实例
        Object serviceLog = serviceLogDecorate.decorate(log);
        serviceLogPersister.persist(serviceLog);
    }
    
    @Override
    public void removeAll() {
        ServiceLoggerSession.getSession().clear();
    }
    
    @Override
    public void setAttribute(String key, Object value) {
        ServiceLoggerSession.getSession().setAttribute(key, value);
    }
}
